import * as network from '@/utils/request';
import $utils from '@/utils/utils';
import { company } from '../../config/Apis';

const request = (uri: string, params: any) => {
    return network.post(uri, {
        data: params,
        requestType: 'form'
    });
}

export async function searchExpressList(params: any): Promise<any> {

    return request(company.expressSearch, params);
}

export async function getExpressList(params: any): Promise<any> {

    return request(company.expressList, params);
}

export async function getExpressDetail(params: any): Promise<any> {

    return request(company.expressDetail, params);
}

export async function createExpress(params: any): Promise<any> {

    return request(company.expressCreate, params);
}

export async function updateExpress(params: any): Promise<any> {

    return request(company.expressUpdate, params);
}

export async function uploadImg(params: any): Promise<any> {
    let formData = new FormData();
    formData.append('image', params.image);
    formData.append('sessionId', params.sessionId);
    return request(company.uploadImg, formData);
}

export async function getExpressByBill(params: any): Promise<any> {

    return request(company.byBillNo, params);
}

export async function getExpressData() {
    let new_data: API.Select[] = [];
    try {
        const msg = await getExpressList({});
        if (msg.code === 0) {
            msg.body.map((item: { id: number, name: string }) => {
                new_data.push({
                    label: item.name,
                    value: item.id,
                });
            });
            $utils.setStorage('express_options', new_data);
        }
    } catch (error) {
        console.log(error);
    }
    return new_data;
};